package com.wwtech.epp.ui.activity.work;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.socks.library.KLog;
import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.base.BaseActivity;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.db.Friend;
import com.wwtech.epp.entity.ProjectEntity;
import com.wwtech.epp.ui.activity.friend.AddPersonActivity;
import com.wwtech.epp.ui.activity.work.component.DaggerProjectReportComponent;
import com.wwtech.epp.ui.activity.work.contract.ProjectReportContract;
import com.wwtech.epp.ui.activity.work.module.ProjectReportModule;
import com.wwtech.epp.ui.activity.work.presenter.ProjectReportPresenter;
import com.wwtech.epp.ui.adapter.work.ProjectAdapter;
import com.wwtech.epp.utils.StringUitl;
import com.wwtech.epp.utils.ToastUtil;
import com.wwtech.epp.utils.UIUtils;
import com.wwtech.epp.view.CustomPopWindow;
import com.wwtech.epp.view.ProjectMemberView;
import com.wwtech.epp.view.datepicker.NumericWheelAdapter;
import com.wwtech.epp.view.datepicker.WheelView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.wwtech.epp.ui.activity.work.NewProjectActivity.CHECKPART;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: $description
 * @date 2017/10/04 10:03:43
 */

public class ProjectReportActivity extends BaseActivity implements ProjectReportContract.View {

    @Inject
    ProjectReportPresenter mPresenter;
    @BindView(R.id.select_project)
    TextView selectProject;
    @BindView(R.id.theme)
    EditText theme;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.content)
    EditText content;
    @BindView(R.id.xietiao_gongzuo)
    EditText xietiaoGongzuo;
    @BindView(R.id.beizhu)
    EditText beizhu;
    @BindView(R.id.recycerView)
    RecyclerView recycerView;
    @BindView(R.id.confirm)
    TextView confirm;
    ProjectAdapter partMamberAdapter;      //参与人的adapter
    private WheelView wheelView;
    private NumericWheelAdapter xiangmuAdapter;

    ArrayList<String> projectIds = new ArrayList<>();

    int projectIdPosition = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_project_report);
        ButterKnife.bind(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        recycerView.setNestedScrollingEnabled(false);
        GridLayoutManager gm1 = new GridLayoutManager(this, 5);
        recycerView.setLayoutManager(gm1);
    }

    @Override
    protected void initData() {
        setTitle("项目汇报");
        if (!getIntent().getStringExtra("title").equals("项目汇报")) {
            selectProject.setText(getIntent().getStringExtra("projectName"));
            selectProject.setClickable(false);
        }
        mPresenter.init();
    }

    @Override
    protected void setupActivityComponent() {
        DaggerProjectReportComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .projectReportModule(new ProjectReportModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(ProjectReportContract.ProjectReportContractPresenter presenter) {
        mPresenter = (ProjectReportPresenter) presenter;
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public void setPersonData(ArrayList<Friend> partList) {
        if (partMamberAdapter == null) {
            partMamberAdapter = new ProjectAdapter(partList);
            recycerView.setAdapter(partMamberAdapter);
            partMamberAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                    ProjectMemberView projectMemberView = (ProjectMemberView) view;
                    if (projectMemberView.isSetInfo()) {
                        ContainValue.partPerson.remove(i);
                        partMamberAdapter.notifyDataSetChanged();
                    } else {
                        ContainValue.partPerson = (ArrayList<Friend>) partMamberAdapter.getData();
                        Intent intent = new Intent(ProjectReportActivity.this, AddPersonActivity.class);
                        intent.putExtra("title", "选择参与人");
                        startActivityForResult(intent, CHECKPART);
                    }
                }
            });
        }
        partMamberAdapter.setNewData(partList);
    }

    @Override
    public void setListData(ProjectEntity projectEntity) {
        ArrayList<String> datalist = new ArrayList<>();
        for (ProjectEntity.DataBean.ListBean s : projectEntity.getData().getList()) {
            datalist.add(s.getProjectName());
            projectIds.add(s.getProjectId() + "");
        }
        xiangmuAdapter = new NumericWheelAdapter(this, 0, datalist.size() - 1, "", datalist);
        showWheelView(selectProject, "选择项目");
    }

    @Override
    public void reportSuccess() {
        if (getIntent().getStringExtra("title").equals("")) {
            EventBus.getDefault().post("add");
        }
        finish();
    }

    private void showWheelView(View showview, String title) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(showview.getWindowToken(), 0);
        View view = LayoutInflater.from(this).inflate(R.layout.wheelview_layout, null);
        LinearLayout ll_content = (LinearLayout) view.findViewById(R.id.ll_content);
        ll_content.setAnimation(AnimationUtils.loadAnimation(this, R.anim.pop_manage_product_in));
        TextView submit = (TextView) ll_content.findViewById(R.id.submit);
        TextView tv_title = (TextView) ll_content.findViewById(R.id.tv_title);
        tv_title.setText(title);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CustomPopWindow.onBackPressed();
                if (xiangmuAdapter.getItemsCount() == 0) {
                    return;
                }
                selectProject.setText(xiangmuAdapter.getItem(wheelView.getCurrentItem()));
                projectIdPosition = wheelView.getCurrentItem();
            }
        });
        wheelView = (WheelView) ll_content.findViewById(R.id.wheelview);
        wheelView.setViewAdapter(xiangmuAdapter);
        new CustomPopWindow.PopupWindowBuilder(this)
                .setClippingEnable(false)
                .setFocusable(true)
                .setView(view)
                .setContenView(ll_content)
                .size(UIUtils.getDisplayWidth(this), UIUtils.getDisplayHeigh(this))
                .create()
                .showAtLocation(showview, Gravity.CENTER, 0, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        mPresenter.onActivityResult(requestCode, resultCode, data);
    }

    @OnClick({R.id.select_project, R.id.confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.select_project:
                mPresenter.getProjectListFromServer();
                break;
            case R.id.confirm:
                if (StringUitl.isNotEmpty(theme, "请输入主题")) {
                    if (StringUitl.isNotEmpty(content, "请输入汇报内容")) {
                        Map<String, String> map = new HashMap<>();
                        map.put("userId", ContainValue.userLogin.getData().getUserId() + "");
                        if (getIntent().getStringExtra("title").equals("项目汇报")) {
                            map.put("projectId", projectIds.get(projectIdPosition));
                        } else {
                            map.put("projectId", getIntent().getStringExtra("projectId"));
                        }
                        map.put("title", theme.getText().toString().trim());
                        if (content.getText().toString().trim().length() > 100) {
                            ToastUtil.displayShortToast("内容超出限制");
                            return;
                        }
                        map.put("content", content.getText().toString().trim());
                        map.put("coordinateJob", xietiaoGongzuo.getText().toString().trim());
                        if (beizhu.getText().toString().trim().length() >= 200) {
                            ToastUtil.displayShortToast("备注文字超出限制");
                            return;
                        }
                        map.put("extrainfo", beizhu.getText().toString().trim());
                        KLog.i(map);
                        if (getIntent().getStringExtra("title").equals("项目汇报")) {
                            mPresenter.report(projectIds.get(projectIdPosition), map);
                        } else {
                            mPresenter.report(getIntent().getStringExtra("projectId"), map);
                        }
                    }
                }
                break;
            default:
                break;
        }
    }
}